package com.xinyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyu.domain.Site;
import com.xinyu.service.ISiteService;
import com.xinyu.service.IUserSiteService;
import com.xinyu.vo.BaseResult;
import com.xinyu.vo.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-16
 */
@RestController
@RequestMapping("/site")
public class SiteController {
    @Autowired
    private ISiteService siteService;

    @GetMapping("/queryAll")
    public ResponseEntity<BaseResult> queryAll() {
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);  // Add this condition
        List<Site> siteList = siteService.list(queryWrapper);
        return ResponseEntity.ok(BaseResult.ok(siteList));
    }

    @PostMapping("/update")
    public ResponseEntity<BaseResult> modify(@RequestBody Site site){
        siteService.updateById(site);
        return ResponseEntity.ok(BaseResult.ok());
    }

    @PostMapping("/save")
    public ResponseEntity<BaseResult> add(@RequestBody Site site){
        siteService.save(site);
        return ResponseEntity.ok(BaseResult.ok());
    }

    @GetMapping("findSiteByUserId/{userId}")
    public ResponseEntity<BaseResult> findSiteByUserId(@PathVariable("userId")Integer userId){
        List<Site> list = siteService.findSiteByUserId(userId);
        return ResponseEntity.ok(BaseResult.ok(list));
    }

    @PostMapping("/list")
    public ResponseEntity<BaseResult> list(@RequestBody(required = false) PageRequest params) {
        PageRequest pageRequest = params == null ? new PageRequest() : params;
        Page<Site> page = new Page<>(pageRequest.getSafePageNum(), pageRequest.getSafePageSize());
        siteService.selectSitePage(page);
        return ResponseEntity.ok(BaseResult.ok(page));
    }

    @DeleteMapping("delete/{id}")
    public ResponseEntity<BaseResult> deleteById(@PathVariable("id")Integer id){
        Site site = siteService.getById(id);
        site.setIsDelete(1);
        siteService.updateById(site);
        return ResponseEntity.ok(BaseResult.ok());
    }


}

